package home.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.facebook.drawee.view.SimpleDraweeView;

import net.ck.hzq.R;
import net.ck.hzq.WebviewActivity;

import java.util.ArrayList;
import java.util.List;

import Utils.ScreenUtil;
import dao.HomeBannerBean;
import widget.banner.Banner;
import widget.banner.transformers.Transformer;

/**
 * @author BF00233
 */

public class CenterBannerAdapter extends DelegateAdapter.Adapter<CenterBannerAdapter.BannerViewHolder> {

    private static final String TAG = "BannerAdapter";

    private final Context mContext;

    private List<List<HomeBannerBean>> mData;

    public CenterBannerAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setUpBanner(List<List<HomeBannerBean>> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
        layoutHelper.setMargin(0, ScreenUtil.dip2px(mContext, 15), 0, ScreenUtil.dip2px(mContext, 15));
        return layoutHelper;
    }

    @NonNull
    @Override
    public BannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_home_center_banner, parent, false);
        return new BannerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final BannerViewHolder holder, int position) {
        if (mData == null) {
            return;
        }
//        holder.banner.setmAutoPlayAble(true);
        holder.banner.setData(mData, null);
        holder.banner.setmAdapter(new Banner.XBannerAdapter() {
            @Override
            public void loadBanner(Banner banner, Object model, View view, int position) {
//                SimpleDraweeView simpleDraweeView=(SimpleDraweeView)view;
                ((SimpleDraweeView) view).setImageURI(Uri.parse(mData.get(position).get(0).getImgBanner()));
//                Glide.with(mContext).load(mData.get(position).get(0).getImgBanner()).into((ImageView) view);
            }
        });
        holder.banner.setPageTransformer(Transformer.Default);
        holder.banner.setOnItemClickListener(new Banner.OnItemClickListener() {
            @Override
            public void onItemClick(Banner banner, int position) {
                WebviewActivity.start(mContext, mData.get(position).get(0).imgPath);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        private Banner banner;

        private BannerViewHolder(View itemView) {
            super(itemView);
            banner = itemView.findViewById(R.id.banner);
        }
    }
}
